{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:10:55.902593Z",
     "start_time": "2025-01-07T03:10:55.893519Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "# 通过 list 构建 Series\n",
    "ser_obj = pd.Series(range(10, 20))\n",
    "print(ser_obj.head(3))"
   ],
   "id": "86373db7538092b7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:11:08.159364Z",
     "start_time": "2025-01-07T03:11:08.149058Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj)",
   "id": "320ed0847debf0c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:11:23.990778Z",
     "start_time": "2025-01-07T03:11:23.979452Z"
    }
   },
   "cell_type": "code",
   "source": "print(type(ser_obj))",
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:13.018916Z",
     "start_time": "2025-01-07T03:12:12.998927Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#获取数据和索引\n",
    "# 获取数据\n",
    "print(ser_obj.values)"
   ],
   "id": "f27557d8e058f44f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:14.812619Z",
     "start_time": "2025-01-07T03:12:14.804788Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取索引\n",
    "print(ser_obj.index)"
   ],
   "id": "a0f15eb7e7a78a9f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:49.704114Z",
     "start_time": "2025-01-07T03:12:49.692456Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#通过索引获取数据\n",
    "print(ser_obj[0])\n",
    "print(ser_obj[8])"
   ],
   "id": "b9f20b37dcf37884",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "18\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:13:35.549833Z",
     "start_time": "2025-01-07T03:13:35.532975Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 索引与数据的对应关系不被运算结果影响\n",
    "print(ser_obj * 2)"
   ],
   "id": "766e45f1b61c8bee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:13:37.664112Z",
     "start_time": "2025-01-07T03:13:37.658574Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj > 15)",
   "id": "a4658e71babbecdb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:24.541678Z",
     "start_time": "2025-01-07T03:14:24.524830Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过 dict 构建 Series\n",
    "year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2.head())"
   ],
   "id": "5154390678436f52",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:35.270318Z",
     "start_time": "2025-01-07T03:14:35.265139Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj2.index)",
   "id": "4927fae49fb4d17a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([2001, 2002, 2003], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:38.003578Z",
     "start_time": "2025-01-07T03:14:37.992780Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj2[2001])",
   "id": "4a77ccd174ec6989",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.8\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:57.254156Z",
     "start_time": "2025-01-07T03:14:57.247515Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# name 属性\n",
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year'\n",
    "print(ser_obj2.head())"
   ],
   "id": "b01bdf3278e6c2ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year\n",
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "Name: temp, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "DataFrame",
   "id": "894971534d06c805"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:16:55.627636Z",
     "start_time": "2025-01-07T03:16:55.618954Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# 通过 ndarray 构建 DataFrame\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4)))\n",
    "print(t)"
   ],
   "id": "b2d5d0314e8f0764",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:17:09.359116Z",
     "start_time": "2025-01-07T03:17:09.353411Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array = np.random.randn(5,4)\n",
    "print(array)"
   ],
   "id": "ffb164e1de99154b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.47054047 -1.72481186  0.48875795 -0.86334711]\n",
      " [-0.46880519  0.5735646  -0.17264354  2.49714303]\n",
      " [-1.76844542 -0.88063625  0.19170642  1.05926568]\n",
      " [-0.13892499 -0.26519704  0.12980134  0.69124726]\n",
      " [ 0.232625   -0.24301078 -0.75228465  0.52098901]]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:17:15.758176Z",
     "start_time": "2025-01-07T03:17:15.740216Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj.head())"
   ],
   "id": "b54f7697a0708771",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.470540 -1.724812  0.488758 -0.863347\n",
      "1 -0.468805  0.573565 -0.172644  2.497143\n",
      "2 -1.768445 -0.880636  0.191706  1.059266\n",
      "3 -0.138925 -0.265197  0.129801  0.691247\n",
      "4  0.232625 -0.243011 -0.752285  0.520989\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:18:59.485207Z",
     "start_time": "2025-01-07T03:18:59.472472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过 dict 构建 DataFrame\n",
    "\n",
    "dict_data = {'A': 1,\n",
    "'B': pd.Timestamp('20190926'),\n",
    "'C': pd.Series(1,index=list(range(4)),dtype='float32'),\n",
    "'D': np.array([3] * 4,dtype='int32'),\n",
    "'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)"
   ],
   "id": "5116863bc47483b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  3       C  wangdao\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:20:14.183694Z",
     "start_time": "2025-01-07T03:20:14.177393Z"
    }
   },
   "cell_type": "code",
   "source": [
    "d1={\"name\":[\"xiaoming\",\"xiaogang\"],\"age\":[20,32],\"tel\":[10086,10010]}\n",
    "print(pd.DataFrame(d1))"
   ],
   "id": "3937f436e694268",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name  age    tel\n",
      "0  xiaoming   20  10086\n",
      "1  xiaogang   32  10010\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:20:29.333909Z",
     "start_time": "2025-01-07T03:20:29.319927Z"
    }
   },
   "cell_type": "code",
   "source": [
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},{ \"name\":\"xiaogang\" ,\"tel\": 10000} ,{\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "print(pd.DataFrame(d2))"
   ],
   "id": "3e6fb4e4d92952d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
